package org.blog.service.impl;

import org.blog.dao.AdminMapper;
import org.blog.entity.Admin;
import org.blog.entity.param.AdminParam;
import org.blog.service.AdminService;
import org.blog.utils.ObjectToMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public List<Admin> search(Admin admin) {
        return null;
    }

    @Override
    public boolean add(Admin admin) {
        return false;
    }

    @Override
    public boolean delete(Integer adminId) {
        int i = adminMapper.deleteByPrimaryKey(adminId);
        if (i > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean update(Admin admin) {
        int i = adminMapper.updateByPrimaryKey(admin);
        if (i > 0) {
            return true;
        }
        return false;
    }

    @Override
    public Admin selectByAdminName(String adminName) {
        Admin admin = adminMapper.selectByAdminName(adminName);
        return admin;
    }

    @Override
    public List<Admin> getAdmins(AdminParam param) {
        Map<String, Object> stringObjectMap = null;
        try {
            stringObjectMap = ObjectToMap.convertBeanToMap(param);
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        List<Admin>  list = adminMapper.getAdmins(stringObjectMap);
        return list;
    }

    @Override
    public Admin getAdminById(Integer adminId) {
        return adminMapper.selectByPrimaryKey(adminId);
    }
}
